package com.higgins.lawapp.server.dataModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceObjectDTO;

@PersistenceCapable
public class DataSourceLevel {
	@SuppressWarnings("unused")
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String dataSourceName;

	@Persistent
	private String dataSourceURL;
	
	@Persistent
	private Boolean dataSourceExecution;
	
	@Persistent
	private Boolean dataSourceConcat;
	
	@Persistent
	private String dataSourceExampleField;
	
	@Persistent
	private List<DataSourceObject> dataSourceObjects;

	public DataSourceLevel()	{
		
	}
	
	public DataSourceLevel(DataSourceLevelDTO dataSourceLevelDTO)	{
		update(dataSourceLevelDTO);
/*		this.dataSourceName = dataSourceLevelDTO.getDataSourceName();
		this.dataSourceURL = dataSourceLevelDTO.getDataSourceURL();
		this.dataSourceExecution = dataSourceLevelDTO.getDataSourceExecution();
		this.dataSourceConcat = dataSourceLevelDTO.getDataSourceConcat();
		this.dataSourceExampleField = dataSourceLevelDTO.getDataSourceExampleField();
		
		List<DataSourceObjectDTO> dataSourceObjectDTOs = dataSourceLevelDTO.getDataSourceObjectDTOs();
		Iterator<DataSourceObjectDTO> iterator = dataSourceObjectDTOs.iterator();
		int dataSourceObjectIndex = 0;
		while(iterator.hasNext())	{
			DataSourceObjectDTO dataSourceObjectDTO = iterator.next();
			if (dataSourceObjectIndex==0)
				dataSourceObjects = new ArrayList<DataSourceObject>();
			
			DataSourceObject dataSourceObject = new DataSourceObject();
			
			if (dataSourceObjectDTO.getDataSourceType().equals("Label"))	{
				dataSourceObject.setDataSourceType("Label");
				dataSourceObject.setDataSourceStart(dataSourceObjectDTO.getStart()); 	// ALTER TESE
				dataSourceObject.setDataSourceEnd(dataSourceObjectDTO.getEnd());		// ALTER THESE
				dataSourceObject.setDataSourceLabel(dataSourceObjectDTO.getLabel());
				dataSourceObject.setDataSourceNumber(dataSourceObjectDTO.getNumber());
				dataSourceObjects.add(dataSourceObject);
			}
			if (dataSourceObjectDTO.getDataSourceType().equals("Element"))	{
				dataSourceObject.setDataSourceType("Element");
				dataSourceObject.setDataSourceStart(dataSourceObjectDTO.getStart());
				dataSourceObject.setDataSourceEnd(dataSourceObjectDTO.getEnd());
				dataSourceObjects.add(dataSourceObject);
			}
			if (dataSourceObjectDTO.getDataSourceType().equals("Field"))	{
				dataSourceObject.setDataSourceType("Field");
				dataSourceObject.setDataSourceStart(dataSourceObjectDTO.getStart());
				dataSourceObject.setDataSourceEnd(dataSourceObjectDTO.getEnd());
				dataSourceObject.setDataSourceFieldName(dataSourceObjectDTO.getFieldName());
				dataSourceObject.setURL(dataSourceObjectDTO.getURL());
				dataSourceObject.setConcat(dataSourceObjectDTO.getConcat());
				dataSourceObjects.add(dataSourceObject);
			}		
			dataSourceObjectIndex++;
		}
		*/
	}
	public List<DataSourceObject> getDataSourceObjects()	{
		return dataSourceObjects;
	}
	public void setDataSourceObjects(List<DataSourceObject> dataSourceObjects)	{
		this.dataSourceObjects = dataSourceObjects;
	}
	
	public void setDataSourceName(String dataSourceName)	{
		this.dataSourceName = dataSourceName;
	}
	public String getDataSourceName()	{
		return dataSourceName;
	}
	public void setDataSourceURL(String dataSourceURL)	{
		this.dataSourceURL = dataSourceURL;
	}
	public String getDataSourceURL()	{
		return dataSourceURL;
	}
	public void setDataSourceExecution(Boolean dataSourceExecution)	{
		this.dataSourceExecution = dataSourceExecution;
	}
	public Boolean getDataSourceExecution()	{
		return dataSourceExecution;
	}
	public void setDataSourceConcat(Boolean dataSourceConcat)	{
		this.dataSourceConcat = dataSourceConcat;
	}
	public Boolean getDataSourceConcat()	{
		return dataSourceConcat;
	}
	public void setDataSourceExampleField(String dataSourceExampleField)	{
		this.dataSourceExampleField = dataSourceExampleField;
	}
	public String getDataSourceExampleField()	{
		return dataSourceExampleField;
	}
	
	
	public DataSourceLevelDTO getDataSourceDTO()	{
		DataSourceLevelDTO dataSourceLevelDTO = new DataSourceLevelDTO();
		dataSourceLevelDTO.setDataSourceName(dataSourceName);
		dataSourceLevelDTO.setDataSourceNameKey(dataSourceName);
		dataSourceLevelDTO.setDataSourceURL(dataSourceURL);
		dataSourceLevelDTO.setDataSourceExecution(dataSourceExecution);
		dataSourceLevelDTO.setDataSourceConcat(dataSourceConcat);
		dataSourceLevelDTO.setDataSourceExampleField(dataSourceExampleField);
		
		Iterator<DataSourceObject> iterator = dataSourceObjects.iterator();
		while (iterator.hasNext())	{
			DataSourceObject dataSourceObject = iterator.next();
			DataSourceObjectDTO dataSourceObjectDTO = dataSourceObject.getDataSourceObjectDTO();
			dataSourceLevelDTO.addDataSourceObjectDTO(dataSourceObjectDTO);
		}
		return dataSourceLevelDTO;
	}
	public void update(DataSourceLevelDTO dataSourceLevelDTO)	{
		this.dataSourceName = dataSourceLevelDTO.getDataSourceName();
		this.dataSourceURL = dataSourceLevelDTO.getDataSourceURL();
		this.dataSourceExecution = dataSourceLevelDTO.getDataSourceExecution();
		this.dataSourceConcat = dataSourceLevelDTO.getDataSourceConcat();
		this.dataSourceExampleField = dataSourceLevelDTO.getDataSourceExampleField();
		
		List<DataSourceObjectDTO> dataSourceObjectDTOs = dataSourceLevelDTO.getDataSourceObjectDTOs();
		Iterator<DataSourceObjectDTO> iterator = dataSourceObjectDTOs.iterator();
		int dataSourceObjectIndex = 0;
		while(iterator.hasNext())	{
			DataSourceObjectDTO dataSourceObjectDTO = iterator.next();
			DataSourceObject dataSourceObject;
			if (dataSourceObjects==null)
				dataSourceObjects = new ArrayList<DataSourceObject>();
			if (dataSourceObjects.size() > dataSourceObjectIndex)
				dataSourceObject = dataSourceObjects.get(dataSourceObjectIndex);
			else	{
				dataSourceObject = new DataSourceObject();
				dataSourceObjects.add(dataSourceObject);
			}
			dataSourceObject.updateDataSourceObject(dataSourceObjectDTO);
			dataSourceObjectIndex++;
		}
	}
}
